package com.devbobcorn.donutopia.world.structure;

import java.util.Random;

import com.devbobcorn.donutopia.Donutopia;
import com.devbobcorn.donutopia.blocks.ModBlocks;
import com.mojang.serialization.Codec;

import net.minecraft.block.BlockState;
import net.minecraft.util.Direction;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.ISeedReader;
import net.minecraft.world.World;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.NoFeatureConfig;

public class CandyCaneFeature extends Feature<NoFeatureConfig> {
	public CandyCaneFeature(Codec<NoFeatureConfig> CODEC) {
		super(CODEC);
	}

	private static final Direction[] DIRECTIONS = { 
		Direction.EAST,
		Direction.WEST,
		Direction.NORTH,
		Direction.SOUTH
	};

	@Override
	public boolean place(ISeedReader world, ChunkGenerator generator, Random random, BlockPos pos, NoFeatureConfig config) {
		BlockState[] CANDIES = { 
			ModBlocks.CANDY_BLOCK.get().defaultBlockState(), 
			ModBlocks.RED_CANDY_BLOCK.get().defaultBlockState(), 
			ModBlocks.ORANGE_CANDY_BLOCK.get().defaultBlockState(), 
			ModBlocks.YELLOW_CANDY_BLOCK.get().defaultBlockState(), 
			ModBlocks.PINK_CANDY_BLOCK.get().defaultBlockState(), 
			ModBlocks.PURPLE_CANDY_BLOCK.get().defaultBlockState()
		};

		int ci = (pos.getX() >> 4) << 4;
		int ck = (pos.getZ() >> 4) << 4;

		RegistryKey<World> dimensionType = world.getLevel().dimension();

		if (dimensionType != RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(Donutopia.MODID, "donutopia")))
			return false;

		if ((random.nextInt(1000000) + 1) <= 50000) {
			int count = random.nextInt(1) + 1;
			for (int a = 0; a < count; a++) {
				int i = ci + random.nextInt(16);
				int k = ck + random.nextInt(16);
				int j = world.getHeight(Heightmap.Type.OCEAN_FLOOR_WG, i, k);
				// Generate our Candy Canes...
				BlockState CANDY1;
				BlockState CANDY2;
				
				Direction dir = DIRECTIONS[random.nextInt(4)];

				if (random.nextBoolean()) {
					int col1 = random.nextInt(6);
					int col2 = random.nextInt(6);
					if (col1 == col2) col2 = (col2 + 1) % 6;
					CANDY1 = CANDIES[col1];
					CANDY2 = CANDIES[col2];
				} else {
					CANDY1 = CANDIES[0];
					CANDY2 = CANDIES[1];
				}
				
				int pillar = random.nextInt(5) + 6;
				int beam = random.nextInt(2) + 3;
				int end = random.nextInt(2) + 1;
				boolean alt = random.nextBoolean();
				Mutable ps = new Mutable(i, j, k);

				// Pillar...
				for (int x = 0;x < pillar;x++){
					world.setBlock(ps, alt ? CANDY1 : CANDY2, 3);
					alt = !alt;
					ps = ps.move(Direction.UP);
				}
				// Beam...
				ps = ps.move(dir);
				alt = !alt;
				for (int x = 0;x < beam;x++){
					world.setBlock(ps, alt ? CANDY1 : CANDY2, 3);
					alt = !alt;
					ps = ps.move(dir);
				}
				// End...
				ps = ps.move(Direction.DOWN);
				alt = !alt;
				for (int x = 0;x < end;x++){
					world.setBlock(ps, alt ? CANDY1 : CANDY2, 3);
					alt = !alt;
					ps = ps.move(Direction.DOWN);
				}
			}
		}
		return true;
	}
}
